package com.gmzg.gmzgoffice.works;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Approval;
import com.gmzg.gmzgoffice.entity.Person;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class LeaveApplicationActivity extends AppCompatActivity {

    private static final String TAG = LeaveApplicationActivity.class.getSimpleName();
    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.result_ll)
    LinearLayout mResultLl;
    @BindView(R.id.approved_ll)
    LinearLayout mApprovedLl;
    @BindView(R.id.state_tv)
    TextView mStateTv;
    @BindView(R.id.namt_tv)
    TextView mNamtTv;
    @BindView(R.id.approve_state_tv)
    TextView mApproveStateTv;
    @BindView(R.id.approve_num_tv)
    TextView mApproveNumTv;
    @BindView(R.id.approve_department_tv)
    TextView mApproveDepartmentTv;
    @BindView(R.id.approve_type_tv)
    TextView mApproveTypeTv;
    @BindView(R.id.approve_days_tv)
    TextView mApproveDaysTv;
    @BindView(R.id.approve_reason_tv)
    TextView mApproveReasonTv;
    @BindView(R.id.approve_remark_tv)
    TextView mApproveRemarkTv;
    @BindView(R.id.activity_approve_detail)
    LinearLayout mActivityApproveDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_application_1);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        setSupportActionBar(mToolbar);
        mToolbarTitle.setText(R.string.leave_application);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        Intent intent = getIntent();
        //获取发送审批时，传过来的数据
        Approval approval = (Approval) intent.getSerializableExtra(Constant.APPROVAL);
        initData(approval);
    }

    private void initData(Approval approval) {
        if (approval != null) {
            //初始化界面的数据
            mNamtTv.setText(approval.getUser_name());
            mApproveNumTv.setText(String.valueOf(approval.getApproval_number()));
            mApproveDepartmentTv.setText(R.string.department);
            mApproveTypeTv.setText(approval.getApproval_type());
            mApproveDaysTv.setText(String.valueOf(approval.getServal_day()));
            mApproveReasonTv.setText(approval.getReason());
            mApproveRemarkTv.setText(approval.getRemark());
            Boolean approval_status = approval.getApproval_Status();
            //我发起时的界面状态
            if (approval_status){
                mStateTv.setText("已审批");
                mApproveStateTv.setText(approval.getWho_approval()+"已审批");
                mStateTv.setText(R.string.pending);
            }else {
                mApproveStateTv.setText("等待"+approval.getWho_approval()+"审批");
                mStateTv.setText("未审批");
            }
            mResultLl.setVisibility(View.GONE);
            mApprovedLl.setVisibility(View.VISIBLE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Approval approval) {
        initData(approval);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

//    int position = intent.getIntExtra("position", -1);
//if (position != -1) {
//        switch (position) {
//        case 0://待我审核的
//        mResultLl.setVisibility(View.VISIBLE);
//        mApprovedLl.setVisibility(View.GONE);
//        break;
//        case 1://我已审核的
//        mResultLl.setVisibility(View.GONE);
//        mApprovedLl.setVisibility(View.VISIBLE);
//        break;
//        case 2://我发起的
//        mResultLl.setVisibility(View.GONE);
//        mApprovedLl.setVisibility(View.VISIBLE);
//        break;
//        }
//        }


//    String id = intent.getStringExtra(Constant.APPROVAL_ID);
//        if (id != null ){
//            BmobQuery<Approval> query = new BmobQuery<>();
//            query.getObject(id, new QueryListener<Approval>() {
//                @Override
//                public void done(Approval approval, BmobException e) {
//                    if (e == null) {
//                        initData(approval);
//                    }
//                }
//            });
//        }